package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyNotificationLogDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyNotificationLog;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYfindInformationReq;
import com.game.server.message.proto.out.XYfindInformationRsp;
import com.game.server.message.proto.out.XYnotificationStruct;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class XYfindInformationReq extends AbstractXYfindInformationReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        LogManager.getInstance().Log("session.getId():"+session.getId());
        if (client != null && client.checkClick(msgcode)) {
            LogManager.getInstance().Log("code:"+msgcode);
            XYfindInformationRsp rsp = new XYfindInformationRsp();
            LogManager.getInstance().Log("code222:"+rsp.toString());
            LogManager.getInstance().Log("this.type:"+this.type);
            rsp.setType(this.type);
            List<XYnotificationStruct> structList = new ArrayList<>();
            if (this.type == 0) {
                List<XyNotificationLog> byMy = XyNotificationLogDao.findByMy(client.getUserId());

                for (XyNotificationLog entity : byMy) {
                    XYnotificationStruct struct = new XYnotificationStruct();
                    String msg = "";
                    LogManager.getInstance().Log("用户id:"+entity.getId());
                    LogManager.getInstance().Log("entity.getCid():"+entity.getCid());
                    LogManager.getInstance().Log("entity.getNotification_type():"+entity.getNotification_type());
                    struct.setId(entity.getId());
                    struct.setCid(entity.getCid());
                    struct.setUid(entity.getUid());
                    struct.setMsgType(entity.getMsg_type());
                    struct.setNotificationType(entity.getNotification_type());
                    //功能未完善  当前只有申请加入俱乐部
                    if (entity.getNotification_type() == 1) {
                        if (entity.getMsg_type() == 1) {
                            msg = "正在申请加入 " + " " + DataManager.getInstance().getMapClub().get(entity.getCid()).getName() + " 俱乐部";

                            JSONObject additionalData = JSONObject.fromObject(entity.getAdditional_data());
                            LogManager.getInstance().Log("数据："+additionalData.toString());
//                            if (additionalData!=null){
//                                LogManager.getInstance().Log("数据：有点的"+additionalData);
//                                Object searchType = additionalData.get("searchType");
//                                if (searchType!=null && Integer.parseInt(searchType.toString())==2){
//                                    msg = msg +" "+ "(专属ID:)"+additionalData.get("searchid").toString();
//                                }
//                            }
                            struct.setMsg(msg);
                            LogManager.getInstance().Log("33===3=3"+DataManager.getInstance().getMapUserData().get(entity.getUid()));
                            struct.setHead(DataManager.getInstance().getMapUserData().get(entity.getUid()).getHead());
                            struct.setName(DataManager.getInstance().getMapUserData().get(entity.getUid()).getName());
                            struct.setState(entity.getState());
                        }
                        if (entity.getMsg_type() == 2) {
                            msg = "申请成为 " + " " + DataManager.getInstance().getMapClub().get(entity.getCid()).getName() + " 俱乐部合伙人";
                            struct.setMsg(msg);
                            LogManager.getInstance().Log("头像："+DataManager.getInstance().getMapUserData().get(entity.getUid()).getHead());
                            struct.setHead(DataManager.getInstance().getMapUserData().get(entity.getUid()).getHead());
                            struct.setName(DataManager.getInstance().getMapUserData().get(entity.getUid()).getName());
                            struct.setState(entity.getState());
                        }
                    }
                    if (entity.getNotification_type() == 2) {
                        if (entity.getMsg_type() == 3) {
                            msg = "正在申请加入 " + " " + DataManager.getInstance().getMapUnion().get(entity.getCid()).getUnion_name() + " 联盟";
                            struct.setMsg(msg);
                            struct.setHead(DataManager.getInstance().getMapClub().get(entity.getUid()).getHeadurl());
                            struct.setName(DataManager.getInstance().getMapClub().get(entity.getUid()).getName());
                            struct.setState(entity.getState());
                        }
                    }
                    structList.add(struct);
                }
            }
            if (this.type == 1) {
                List<XyNotificationLog> byClub = XyNotificationLogDao.findByClub(this.id);
                for (XyNotificationLog entity : byClub) {
                    XYnotificationStruct struct = new XYnotificationStruct();
                    String msg = "";
                    struct.setId(entity.getId());
                    struct.setCid(entity.getCid());
                    struct.setUid(entity.getUid());
                    struct.setMsgType(entity.getMsg_type());
                    struct.setNotificationType(entity.getNotification_type());
                    //功能未完善  当前只有申请加入俱乐部
                    if (entity.getNotification_type() == 1) {
                        if (entity.getMsg_type()!=3){
                            struct.setHead(DataManager.getInstance().getMapUserData().get(entity.getUid()).getHead());
                            struct.setName(DataManager.getInstance().getMapUserData().get(entity.getUid()).getName());
                            struct.setState(entity.getState());
                        }

                        if (entity.getMsg_type() == 1) {
                            msg = " 正在申请加入 " + " " + DataManager.getInstance().getMapClub().get(entity.getCid()).getName() + " 俱乐部";
                            JSONObject additionalData = JSONObject.fromObject(entity.getAdditional_data());
                            if (additionalData!=null && additionalData.size()>0){
                                Object searchType = additionalData.get("searchType");
                                if (searchType!=null && Integer.parseInt(searchType.toString())==2){
                                    msg = msg +" "+ "(专属ID:)"+additionalData.get("searchid").toString();
                                }
                            }
                            struct.setMsg(msg);
                        }
                        if (entity.getMsg_type() == 2) {
                            msg = " 正在申请成为俱乐部 合伙人";
                            struct.setMsg(msg);
                        }

                    }
                    if (entity.getNotification_type() == 2) {
                        if (entity.getMsg_type() == 3) {
                            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(entity.getUid());
//                            XyClubMemberInfo memberInfo = clubInfo.getPlayer().get(clubInfo.getCreatorid());
                            struct.setHead(clubInfo.getHeadurl());
                            struct.setName(clubInfo.getName());
                            struct.setState(entity.getState());
                            msg = " 俱乐部 "+ clubInfo.getName() +" 正在申请加入联盟";
                            struct.setMsg(msg);
                        }
                    }
                    structList.add(struct);
                }
            }

            XYnotificationStruct[] tempStruct = new XYnotificationStruct[10];
            int page = 0;
            int index = 0;
            for (XYnotificationStruct entity : structList) {
                tempStruct[index] = entity;
                page++;
                index++;
                if (page % 10 == 0) {
                    rsp.setIsend(1);
                    rsp.setNotifications(tempStruct);
                    client.Send(rsp);
                    tempStruct = new XYnotificationStruct[10];
                    index = 0;
                }
            }
            rsp.setIsend(0);
            rsp.setNotifications(tempStruct);
            client.Send(rsp);
        }
    }

}
